/*
 * Copyright (C) 2009 Rob Cash
 * 
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 *     http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 * 
 * DefaultState.java
 * 
 * Created on Jan 1, 2009 at 12:49:10 PM
 */

package org.robcash.commons.util;

/**
 * Default implementation of a state for a state machine
 * @param <T> Type of value contained by this state
 * @author Rob
 */
public class DefaultState<T> implements State<T> {

    /**
     * Name of state
     */
    private final String name;

    /**
     * Value of state
     */
    private T value;

    /**
     * Create a new default state with no value
     * @param name Name of state
     */
    public DefaultState(String name) {
        this.name = name;
    }

    /**
     * Create a new default state with the specified name and value
     * @param name Name of state
     * @param value Value of state
     */
    public DefaultState(String name, T value) {
        this(name);
        this.value = value;
    }

    /**
     * Get the name of the state
     * @return Name of the state
     */
    public String getName() {
        return name;
    }

    /**
     * Get the value of the state
     * @return Value of the state
     */
    public T getValue() {
        return value;
    }

    /**
     * Set the value of the state
     * @param value New value for state
     */
    public void setValue(T value) {
        this.value = value;
    }

    /**
     * Indicates if this state is equal to another. States are equal if their
     * names and values are equal
     * @param obj Another object to test for equality
     * @return <tt>true</tt> if the objects are equal; <tt>false</tt> otherwise
     */
    @Override
    public boolean equals(Object obj) {
        if ( !(obj instanceof DefaultState) )
        {
            return false;
        }

        final DefaultState<T> other = (DefaultState<T>) obj;
        if ( (name != null && name.equals(other.name)) || (name == null && other.name == null) )
        {
            // Names are the same
            if ( (value != null && value.equals(other.value)) || (value == null && other.value == null) )
            {
                return true;
            }
        }

        return false;
    }

    @Override
    public int hashCode() {
        int hash = 7;
        hash = 97 * hash + (this.name != null ? this.name.hashCode() : 0);
        hash = 97 * hash + (this.value != null ? this.value.hashCode() : 0);
        return hash;
    }

}
